<template>
  <van-form>
    <van-field
      v-model="vaccinator.name"
      name="姓名"
      label="姓名"
      placeholder="接种者姓名"
    />
    <van-field
      v-model="vaccinator.idCard"
      type="text"
      name="身份证号"
      label="身份证号"
      placeholder="请输入身份证号"
      :rules="[{ validator, message: '请输入正确身份证号' }]"
    />
    <van-field
      v-model="recard"
      type="text"
      name="确认身份证号"
      label="确认身份证号"
      placeholder="请再一次输入身份证号"
      :rules="[{ validator2, message: '请输入正确身份证号' }]"
    />
    <van-field
      v-model="vaccinator.age"
      type="tel"
      name="年龄"
      label="年龄"
      placeholder="请输入年龄"
    />
    <van-field name="radio" label="性别">
      <template #input>
        <van-radio-group v-model="vaccinator.sex" direction="horizontal">
          <van-radio name="1">男</van-radio>
          <van-radio name="2">女</van-radio>
        </van-radio-group>
      </template>
    </van-field>

    <div style="margin: 16px;">
      <van-button round block type="info" native-type="submit" @click="saveOrUpdate()">添加信息</van-button>
    </div>
  </van-form>
</template>

<script>
import vaccinatorApi from '@/api/vaccinator'
import { Toast } from 'vant'
import Cookies from 'js-cookie'
export default {
  name: 'Form',
  data() {
    return {
      pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
      vaccinator: {
        id: '',
        idCard: '',
        sex: '',
        age: '',
        name: '',
        sysUserId: ''
      },
      recard: '',
      saveBtnDisabled: false
    }
  },
  created() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id)
      this.vaccinator.id = this.$route.params.id
    }
  },
  methods: {
    validator(val) {
      if (parseInt(val.substr(16, 1)) % 2 === 1) {
        this.vaccinator.sex = '1'
      } else {
        this.vaccinator.sex = '2'
      }
      var yearBirth = val.substring(6, 10)
      var monthBirth = val.substring(10, 12)
      var dayBirth = val.substring(12, 14)
      // 获取当前年月日并计算年龄
      var myDate = new Date()
      var monthNow = myDate.getMonth() + 1
      var dayNow = myDate.getDay()
      this.vaccinator.age = myDate.getFullYear() - yearBirth
      if (monthNow < monthBirth || (monthNow === monthBirth && dayNow < dayBirth)) {
        this.vaccinator.age--
      }

      this.vaccinator.sysUserId = Cookies.get('Sysid')
      console.log(this.vaccinator.sysUserId)
      return this.pattern.test(val)
    },
    validator2(val) {
      return this.vaccinator.idCard === val
    },
    saveOrUpdate() {
      // 禁用保存按钮
      this.saveBtnDisabled = true
      if (!this.vaccinator.id) {
        this.onSubmit()
      } else {
        this.updateData()
      }
    },

    onSubmit() {
      if (this.vaccinator.mobile === '' ||
        this.vaccinator.username === '' ||
        this.vaccinator.idCard === '' ||
        this.recard === '' ||
        this.vaccinator.password === '') {
        this.saveBtnDisabled = false
        Toast.fail('有空项目不能提交')
      } else {
        // debugger
        vaccinatorApi.save(this.vaccinator).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          Toast.success('有空项目不能提交')
          this.$router.push({ path: '/vaccinatorList' })
        })
      }
    },
    // 根据id查询记录
    fetchDataById(id) {
      vaccinatorApi.getById(id).then(response => {
        this.vaccinator = response.data.item
      })
    },
    // 根据id更新记录
    updateData() {
      // vaccinator数据的获取
      vaccinatorApi.updateById(this.vaccinator).then(response => {
        this.$message({
          type: 'success',
          message: response.message
        })
        this.$router.push({ path: '/vaccinatorList' })
      })
    }
  }
}

</script>

<style scoped>

</style>
